package com.hyh.ui.contacts;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.hyh.media.AudioRecordUtil;
import com.hyh.ui.base.BaseActivity;
import com.hyh.ui.localnumber.ShowImgActivity;
import com.hyh.util.FloatWindowUtil;
import com.hyh.util.SharePreferenceUtil;
import com.hyh.util.SoundUtil;
import com.hyh.util.dbg;
import com.hyh.videochat.R;
import com.platform.contact.UserInfo;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2019/1/26.
 */

public class CommActivity extends BaseActivity<CommPresenter> implements CommContract.View {

    @BindView(R.id.iv_icon)
    ImageView ivIcon;
    @BindView(R.id.et_contacts_phone_num)
    EditText etContactsNum;
    @BindView(R.id.tv_local_number)
    TextView tvLocalNumber;
    @BindView(R.id.btn_confirm)
    Button btnConfirm;
    boolean recordFlag;

    public static void startActivity(Activity activity){
        Intent intent = new Intent(activity,CommActivity.class);
        activity.startActivity(intent);
    }

    @Override
    public int getLayout() {
        return R.layout.activity_contacts;
    }

    @Override
    public void getIntentExtras(Intent intent) {
    }

    @Override
    protected void onStart() {
        super.onStart();
//        FloatWindowUtil.show(this);
    }

    @Override
    protected void onStop() {
        super.onStop();
        dbg.e("后台播放无声音乐");
        SoundUtil.getInstance(this).play();
    }

    @Override
    public void initDataAndEvent() {

        String localNum = SharePreferenceUtil.getString(SharePreferenceUtil.Contact.LocalNumber);
        String contactsNum = SharePreferenceUtil.getString(SharePreferenceUtil.Contact.ContactsNumber);
        recordFlag=SharePreferenceUtil.getBoolean(SharePreferenceUtil.Contact.RecordFlag);
        if(localNum!=null){
            tvLocalNumber.setText("本机号码:"+localNum);
        }
        if(contactsNum!=null){
            UserInfo.toId = contactsNum;
            etContactsNum.setText(contactsNum);
        }

        if(recordFlag&&contactsNum!=null){
            AudioRecordUtil.getInstance().startRecord();
            btnConfirm.setText("停止通话");
        }else {
            btnConfirm.setText("开始通话");
        }
        Animation animation = AnimationUtils.loadAnimation(this,R.anim.translate_image);
        ivIcon.setAnimation(animation);
    }

    @OnClick({R.id.btn_confirm,R.id.iv_contacts,R.id.iv_icon})
    public void onClick(View view){
        switch (view.getId()){
            case R.id.btn_confirm:
                if(recordFlag){
                    recordFlag = false;
                    btnConfirm.setText("开始通话");
                    SharePreferenceUtil.save(SharePreferenceUtil.Contact.RecordFlag,false);
                    AudioRecordUtil.getInstance().stopRecord();
                }else{
                    String contactsNum = etContactsNum.getText().toString().trim();
                    if(contactsNum.length()<11){
                        Toast.makeText(this,"手机号码格式不正确",Toast.LENGTH_SHORT).show();
                        return;
                    }
                    recordFlag = true;
                    btnConfirm.setText("停止通话");
                    UserInfo.toId = contactsNum;
                    SharePreferenceUtil.save(SharePreferenceUtil.Contact.ContactsNumber,contactsNum);
                    AudioRecordUtil.getInstance().startRecord();
                    SharePreferenceUtil.save(SharePreferenceUtil.Contact.RecordFlag,true);
                }
                break;
            case R.id.iv_contacts:
                // 跳转到联系人界面
                Intent intent = new Intent();
                intent.setAction("android.intent.action.PICK");
                intent.addCategory("android.intent.category.DEFAULT");
                intent.setType("vnd.android.cursor.dir/phone_v2");
                startActivityForResult(intent, 0x30);
                break;
            case R.id.iv_icon:
                ShowImgActivity.startActivity(this,R.drawable.address_book_father_big);
                break;
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==0x30) {
            if (data != null) {
                Uri uri = data.getData();
                String phoneNum = null;
                String contactName = null;
                // 创建内容解析者
                ContentResolver contentResolver = getContentResolver();
                Cursor cursor = null;
                if (uri != null) {
                    cursor = contentResolver.query(uri,
                            new String[]{"display_name","data1"}, null, null, null);
                }
                while (cursor.moveToNext()) {
                    contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                    phoneNum = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                }
                cursor.close();
                //  把电话号码中的  -  符号 替换成空格
                if (phoneNum != null) {
                    phoneNum = phoneNum.replaceAll("-", " ");
                    // 空格去掉  为什么不直接-替换成"" 因为测试的时候发现还是会有空格 只能这么处理
                    phoneNum= phoneNum.replaceAll(" ", "");
                }
                etContactsNum.setText(phoneNum);
                etContactsNum.setSelection(phoneNum.length());
                btnConfirm.setText("开始通话");
                AudioRecordUtil.getInstance().stopRecord();
                recordFlag = false;
            }
        }
    }

    @Override
    public void setContracts(final String id) {
        if(!recordFlag&&!etContactsNum.getText().toString().equals(id)){
            etContactsNum.setText(id);
            etContactsNum.setSelection(id.length());
        }
    }
}
